<?php
// +----------------------------------------------------------------------
// | ThinkSNS
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.thinksns.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: nonant <nonant@thinksns.com>
// +----------------------------------------------------------------------
//

class SpaceModel extends Model{

	static private $_instance = null;

	private function getSpaceInfoData($uid){
         if(self::$_instance === null)  
         {  
             self::$_instance = $this->table(C('DB_PREFIX')."space_info")->where("uid=$uid")->field('uid,baseinfo,extend,listinfo,showfields,colleaguegroup')->find();
         }
         return self::$_instance;
	}
	
	//获取用户数据
	function get_data($uid){
		$list['baseinfo'] = $this->get_Base_Data($uid);
		$list['extend'] = $this->get_Expend_Data($uid);
		$list['listinfo'] = $this->get_ListInfo_Data($uid);
		return $list;
	}

	//获取空间信息
	function get_space($uid){
		$indexField = $this->get_Space_field($uid);
		$field      = $this->info_Field_list();
		$spaceInfo = $this->get_data($uid);
		foreach ($spaceInfo as $key=>$value){
			foreach ($value as $k=>$v){
				//if(in_array($k,$indexField[$key]) || $field[$key][$k]['need']==true){
				if(in_array($k,$indexField[$key]) || $spaceInfo[$key][$k]['need']==true){
					$field[$key][$k] = $v;
				}else{
					unset($field[$key][$k]);
				}
			}
		}
		return $field;
	}


	/**
	 +----------------------------------------------------------
	 * Description 保存显示在个人空间中的字段信息
	 +----------------------------------------------------------
	 * @author Nonant nonant@thinksns.com
	 +----------------------------------------------------------
	 * @param $uid 用户ID
	 * @param $type 类型
	 * @param $space 需要保存到个人空间的字段数组
	 +----------------------------------------------------------
	 * @return boolen
	 +----------------------------------------------------------
	 * Create at  2010-1-21 下午04:27:15
	 +----------------------------------------------------------
	 */
	protected function save_Space_data($uid,$type,$space){
		$spacedata = array();
		$spacedataget = $this->table(C('DB_PREFIX')."space_info")->where("uid=$uid")->field('showfields')->find();
		$spacedata = unserialize($spacedataget['showfields']);
		$spacedata[$type] = $space;
		$spacedata = serialize($spacedata);
		self::$_instance === null;
		return $this->query("UPDATE ".C('DB_PREFIX')."space_info SET showfields='$spacedata' WHERE uid='$uid'");
	}

	/**
	 +----------------------------------------------------------
	 * Description 显示在空间的 字段信息
	 +----------------------------------------------------------
	 * @author Nonant nonant@thinksns.com
	 +----------------------------------------------------------
	 * @param Int $uid 用户ID
	 * @param String $type 字段集类型 baseinfo=基本信息 extend=扩展信息
	 +----------------------------------------------------------
	 * @return Array 如果$type为空，则返回所有的信息
	 +----------------------------------------------------------
	 * Create at  2010-1-21 上午11:44:53
	 +----------------------------------------------------------
	 */
	public function get_Space_field($uid,$type){
		$data = $this->getSpaceInfoData($uid);
		$list = unserialize($data['showfields']);
		if($type){
			return $list[$type];
		}else{
			return $list;
		}
	}

	//保存search信息
	public function save_Search_Data($uid,$type,$data){
		$info = $this->info_Field_list($type);
		$this->query("DELETE FROM ".C('DB_PREFIX')."space_search WHERE uid=$uid AND type='$type'");
		foreach ($data as $key=>$value){
			if($value) $arr[] = "($uid,'".$info[$key]['info']."','$key','$value','$type')";
		}
		$values = implode(',',$arr);
		$this->query("INSERT INTO ".C('DB_PREFIX')."space_search (uid,field,info,value,type) values $values");
	}


	//获取基本信息
	public function get_Base_Data($uid){
		$list = $this->info_Field_list('baseinfo');
		$data = $this->getSpaceInfoData($uid);
		$outdata = unserialize($data['baseinfo']);
		foreach ($list as $key=>$value){
			$list[$key]['value'] = $outdata[$key];
			$list[$key]['field'] = $key;
		}
		return $list;
	}

	//保存基本
	public function save_Base_Data($uid,$type,$value,$index){
		foreach ($value as $key=>$val){
			if($val) $savedata[$key]=$val;
		}
		$this->save_Search_Data($uid,$type,$savedata);
		$this->save_Space_data($uid,'baseinfo',$index);
		$this->query("UPDATE ".C('DB_PREFIX')."user SET fullname='$value[username]',sex='$value[usersex]' WHERE uid='$uid'");
		self::$_instance = null;
		return $this->query("UPDATE ".C('DB_PREFIX')."space_info SET baseinfo='".serialize($savedata)."' WHERE uid='$uid'");
	}

	//获取扩展备注信息
	public function get_Expend_Data($uid,$type){
		$list = $this->info_Field_list('extend');
		$data = $this->getSpaceInfoData($uid);
		$data = unserialize($data['extend']);
		if($type){
			$list[$type]['value'] = $data[$type];
			$list[$type]['field'] = $type;
			$out = $list[$type];
		}else{
			foreach ($list as $key=>$val){
				$list[$key]['value'] = $data[$key];
				$list[$key]['field'] = $key;
			}
			$out = $list;
		}
		return $out;
	}

	//保存扩展备份信息
	public function save_Expend_Data($uid,$key,$value,$index){
		$savedata = $this->getSpaceInfoData($uid);
		$savedata = unserialize($savedata['extend']);
		$savedata[$key] = $value;

		$this->save_Search_Data($uid,'extend',$savedata);
		$this->save_Space_data($uid,'extend',$index);
		self::$_instance = null;
		return $this->query("UPDATE ".C('DB_PREFIX')."space_info SET extend='".serialize($savedata)."' WHERE uid='$uid'");
	}

	//获取列表扩展信息
	public function get_ListInfo_Data($uid,$type){
		$list = $this->info_Field_list('listinfo');
		$data = $this->getSpaceInfoData($uid);
		$data = unserialize($data['listinfo']);
		return ($type)?$data[$type]:$data;
	}

	//保存列表扩展信息
	public function save_ListInfo_Data($uid,$key,$value){
		$list = $this->info_Field_list('listinfo');
		$data = $this->getSpaceInfoData($uid);
		$savedata = $data['listinfo'];
		$savedata = unserialize($savedata['listinfo']);
		$savedata[$key] = $value;
		//return $this->save_Search_Data($uid,'listinfo',$o);  扩展表信息暂没实现保存search表
		self::$_instance = null;
		return $this->query("UPDATE ".C('DB_PREFIX')."space_info SET listinfo='".serialize($savedata)."' WHERE uid='$uid'");
	}

	/**
	 +----------------------------------------------------------
	 * Description 用户信息字段设段中心
	 +----------------------------------------------------------
	 * @author Nonant nonant@thinksns.com
	 +----------------------------------------------------------
	 * @param unknown_type $type
	 +----------------------------------------------------------
	 * @return
	 +----------------------------------------------------------
	 * Create at  2010-2-4 下午03:51:32
	 +----------------------------------------------------------
	 */
	public function info_Field_list($type){
		$list = array(
			'baseinfo' => array(
					//'shortname'   =>  array( 'info'  => '帐号' ),
					'username'    =>  array( 'info'  => '姓名' , 'need'=>true),
		    		'englishname' =>  array( 'info'  => '英文名' ),		
		    		'cardId'      =>  array( 'info'  => '工号' , 'need'=>true),
					'usersex'     =>  array( 'info'  => '性别' , 'need'=>true),
		    		'department'  =>  array( 'info'  => '部门' , 'need'=>true),
					'userphone'   =>  array( 'info'  => '电话' , 'need'=>true),
					'usermobile'  =>  array( 'info'  => '手机' ),
					'userroom'    =>  array( 'info'  => '座位' , 'need'=>true),	
					
		    		//'userlocation'=>  array( 'info'  => '工作地区' ),
		    		'useremail'   =>  array( 'info'  => '邮箱' ),
		    		'userfax'     =>  array( 'info'  => '传真'),

			),
			'extend' => array(
					'experience'  =>  array( 'info'  => '项目经验' ),
					'organise'    =>  array( 'info'  => '架构体系' ),
			),
			'listinfo'=>array(
				    'workinfo'    => array( 'info'  => '工作经历' , 'recombine' => '{company} - {dept} - {time}' ),
			)
		);
		return $list[$type];
	}

}
?>